package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaResponse;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosResponse;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Service
public class VodServiceImpl implements VodService {

    //注入vod属性类
    @Resource
    private VodConstantProperties vodConstantProperties;

    /**
     *
     * @param mediaFileId
     */
    @Override
    public void deleteMedia(String mediaFileId) {
        // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

        // 实例化要请求产品的client对象,clientProfile是可选的
        VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DeleteMediaRequest req = new DeleteMediaRequest();
        req.setFileId(mediaFileId);
        // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
        try {
            DeleteMediaResponse resp = client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据流媒体ID，获取声音
     * @param mediaFileId
     * @return
     */
    @Override
    public TrackMediaInfoVo getMediaInfo(String mediaFileId) {
        // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

        // 实例化要请求产品的client对象,clientProfile是可选的
        VodClient client = new VodClient(cred, "");
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
        String[] fileIds1 = {mediaFileId};
        req.setFileIds(fileIds1);

        // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
        DescribeMediaInfosResponse resp = null;
        try {
            resp = client.DescribeMediaInfos(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }

        //获取返回结果
        if (resp.getMediaInfoSet().length>0) {
            //如果流媒体ID，有对应信息，则获取，并封装成TrackMediaInfoVo
            TrackMediaInfoVo trackMediaInfoVo=new TrackMediaInfoVo();
            trackMediaInfoVo.setMediaUrl(resp.getMediaInfoSet()[0].getBasicInfo().getMediaUrl());
            trackMediaInfoVo.setDuration(resp.getMediaInfoSet()[0].getMetaData().getDuration());
            trackMediaInfoVo.setSize(resp.getMediaInfoSet()[0].getMetaData().getSize());
            trackMediaInfoVo.setType(resp.getMediaInfoSet()[0].getBasicInfo().getType());
            //返回结果
            return trackMediaInfoVo;
        }
        //没有对应流媒体，返回null
        return null;
    }

    /**
     * 上传声音
     * @param file
     * @return
     */
    @Override
    public Map<String, Object> uploadTrack(MultipartFile file) {
        //上传文件-》腾讯云点播vod ->返回文件id,和文件URL
        //1、本地临时路径：如xx.mp3->F：/tingshuTrack/yyyyMMdd/uuid.mp3
        String mediaFilePath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
        //2、使用腾讯云点播vod，上传音频
        // 2.1实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

        //2.2初始化一个上传客户端对象
        VodUploadClient client = new VodUploadClient(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

        //2.3构造上传请求对象
        //设置媒体本地上传路径。
        VodUploadRequest request = new VodUploadRequest();
        request.setMediaFilePath(mediaFilePath);
        request.setProcedure(vodConstantProperties.getProcedure());
        //2.4调用上传方法，传入接入点地域及上传请求。
        VodUploadResponse response = null;
        try {
            response = client.upload(vodConstantProperties.getRegion(), request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //3.获取上传结果
        HashMap<String, Object> map = new HashMap<>();
        map.put("mediaFileId", response.getFileId());
        map.put("mediaUrl",response.getMediaUrl());
        //返回结果
        return map;
    }


}
